package com.cn.mobileproject.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.cn.mobileproject.Configuration;
import com.cn.mobileproject.R;
import com.cn.mobileproject.manager.Manager;
import com.cn.mobileproject.utils.ActivityUtils;
import com.cn.mobileproject.view.adapter.AppAdapter;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;

public class HomeActivity extends HeaderActivity {

	
	protected GridView mGridView;
	protected AppAdapter adapter;
	
	@Override
	protected void initComponent() {
		super.initComponent();
		mGridView = (GridView) this.findViewById(R.id.home_grid);
		int width =  getWindowManager().getDefaultDisplay().getWidth();
		adapter = new AppAdapter(this,width);
		mGridView.setAdapter(adapter);
	}

	@Override
	protected void initComponentValue() {
		super.initComponentValue();
		if(null!=adapter) adapter.notifyDataSetChanged();
	}

	@Override
	protected void initEvent() {
		super.initEvent();
		mGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				switch(arg2){
				case 0:
					ActivityUtils.goToActivityNoFinish(HomeActivity.this, ProjectListActivity.class);
					break;
				case 1:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 2:
					MobclickAgent.onEvent(HomeActivity.this, Configuration.PHOTO);
					ActivityUtils.goToActivityNoFinish(HomeActivity.this, UploadImageActivity.class);
					break;
				case 3:
					
					break;
				case 4:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 5:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 6:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 7:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 8:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 9:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 10:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 11:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 12:
					ActivityUtils.goToActivityNoFinish(HomeActivity.this, TasksActivity.class);
					break;
				case 13:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 14:
//					Toast.makeText(HomeActivity.this, "onItem:"+arg2, Toast.LENGTH_SHORT).show();
					break;
				case 15:
					ActivityUtils.goToActivityNoFinish(HomeActivity.this, MessageListActivity.class);
					break;
				}
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.home_activity);
		initComponent();
		initEvent();
		UmengUpdateAgent.update(this);
	}
	
	public void onStart() {
		super.onStart();
		handler.postDelayed(runnable, 10 * 1000);
	}

	public void onPause() {
		super.onPause();
		handler.removeCallbacks(runnable);
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			initComponentValue();
		}
	};

	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			Manager.getInstance().initMessage();
			handler.postDelayed(this, 30*60*1000);
		}
	};
	
}
